package com.rongc.client.freight.business.supply.view.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.rongc.client.core.gson.GsonHelper;
import com.rongc.client.core.network.code.HandleCode;
import com.rongc.client.core.network.http.RequestManager;
import com.rongc.client.core.utils.LogUtils;
import com.rongc.client.freight.R;
import com.rongc.client.freight.base.BaseFragment;
import com.rongc.client.freight.base.view.widget.DividerItemDecoration;
import com.rongc.client.freight.base.view.widget.EmptyRecyclerView;
import com.rongc.client.freight.business.mine.request.api.MineGetReplyApi;
import com.rongc.client.freight.business.mine.request.api.MineReplyApi;
import com.rongc.client.freight.business.supply.model.CommentBean;
import com.rongc.client.freight.business.supply.view.adapter.RecyclerSupplyCommentAdapter;
import com.rongc.client.freight.utils.UtilMethod;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by Bruce on 2016/11/24.
 */
public class SupplyCommentListFragment extends BaseFragment {
    public static final int COMMENT_REPLY = 1;
    public static final int COMMENT_GETREPLY = 2;

    @Bind(R.id.id_empty_view)
    View mEmptyView;
    @Bind(R.id.recycler)
    EmptyRecyclerView mRecyclerView;
    RecyclerSupplyCommentAdapter modelAdapter;

    List<CommentBean> datas;
    int type;

    public static SupplyCommentListFragment getInstance(){
        SupplyCommentListFragment fragment = new SupplyCommentListFragment();
        Bundle bdl = new Bundle();
//        bdl.putString("orderType", orderType);
        fragment.setArguments(bdl);
        return fragment;
    }

    public static SupplyCommentListFragment getInstance(int type){
        SupplyCommentListFragment fragment = new SupplyCommentListFragment();
        Bundle bdl = new Bundle();
//        bdl.putString("orderType", orderType);
        fragment.setArguments(bdl);
        fragment.type = type;
        return fragment;
    }

    Response.Listener<JSONObject> respOrderListener = new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            if (HandleCode.requestSuccess()) {
                try {
                    CommentBean[] commenta = GsonHelper.getDeserializer().fromJson(response.getString("list"), CommentBean[].class);
                    datas.clear();
                    if(commenta != null)
                        for (int i=0;i<commenta.length;i++){
                            datas.add(commenta[i]);
                        }
                    modelAdapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                UtilMethod.dismissProgressDialog(getActivity());
            }
        }
    };

    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            LogUtils.i("error.getMessage(): " + error.getMessage());
            UtilMethod.dismissProgressDialog(getActivity());
        }
    };

    public void setDatas(List<CommentBean> datas){
        this.datas.clear();
        if(datas!=null)
            this.datas.addAll(datas);
        modelAdapter.notifyDataSetChanged();
    }


    @Override
    public void initData() {
        switch (type){
            case COMMENT_GETREPLY:
                RequestManager.getInstance().call(new MineGetReplyApi(
                        new MineGetReplyApi.MineGetReplyParams(),
                        respOrderListener, errorListener));
                break;
            case COMMENT_REPLY:
                RequestManager.getInstance().call(new MineReplyApi(
                        new MineReplyApi.MineReplyParams(),
                        respOrderListener, errorListener));
                break;
        }

    }

    @Override
    public void initListener() {

    }


    @Override
    public void initView() {
        datas = new ArrayList<>();

        modelAdapter = new RecyclerSupplyCommentAdapter(getActivity(),datas);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
        mRecyclerView.setAdapter(modelAdapter);
        mRecyclerView.setEmptyView(mEmptyView);
    }


    @Override
    public int getLayoutId() {
        return R.layout.fragment_comment;
    }
}
